package com.nutricia.biz.convertor;

import com.nutricia.common.api.info.CmsMenuInfo;
import com.nutricia.common.api.info.MenuTreeInfo;
import com.nutricia.common.api.info.SubMenuInfo;
import com.nutricia.common.api.info.SubMenuTreeInfo;
import com.nutricia.core.model.domain.CmsSystemMenuDO;
import com.nutricia.core.model.domain.SubSystemMenuDO;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 系统菜单转换去器。DO和DTO或者INFO的相关转换
 *
 * @author: huangsheng
 * @date: 2017/10/20
 */
public class SystemMenuConvertor {


    /**
     * 将 CmsSystemMenuDOs 转换为 MenuTreeInfo
     *
     * @author: huangsheng
     * @date: 2017/10/20 下午2:00
     */
    public static List<MenuTreeInfo> convertToMenuTreeInfos(final List<CmsSystemMenuDO> cmsSystemMenuDOS) {
        if (CollectionUtils.isNotEmpty(cmsSystemMenuDOS)) {
            List<MenuTreeInfo> menuTreeInfos = new ArrayList<>(cmsSystemMenuDOS.size());
            cmsSystemMenuDOS.forEach(cmsSystemMenuDO -> {
                //实例化菜单数据
                MenuTreeInfo menuTreeInfo = new MenuTreeInfo(cmsSystemMenuDO.getId(), cmsSystemMenuDO.getMenuName());
                // 实例化二级菜单
                List<SubSystemMenuDO> subSystemMenuDOS = cmsSystemMenuDO.getSubSystemMenuDOS();
                if (CollectionUtils.isNotEmpty(subSystemMenuDOS)) {
                    List<SubMenuTreeInfo> subMenuTreeInfos = new ArrayList<>(subSystemMenuDOS.size());
                    subSystemMenuDOS.forEach(subSystemMenuDO -> {
                        SubMenuTreeInfo subMenuTreeInfo = new SubMenuTreeInfo();
                        // 设置二级菜单名称
                        subMenuTreeInfo.setMenuId(subSystemMenuDO.getId());
                        subMenuTreeInfo.setLabel(subSystemMenuDO.getMenuName());
                        subMenuTreeInfos.add(subMenuTreeInfo);
                    });
                    menuTreeInfo.setChildren(subMenuTreeInfos);
                } else {
                    menuTreeInfo.setChildren(Collections.emptyList());
                }
                menuTreeInfos.add(menuTreeInfo);
            });
            return menuTreeInfos;
        }
        return Collections.emptyList();
    }


    /**
     * 将 CmsSystemMenuDOs 转换为 CmsMenuInfos
     *
     * @author: huangsheng
     * @date: 2017/10/20 下午2:00
     */
    public static List<CmsMenuInfo> convertToCmsMenuInfos(final List<CmsSystemMenuDO> cmsSystemMenuDOS) {
        if (CollectionUtils.isNotEmpty(cmsSystemMenuDOS)) {
            List<CmsMenuInfo> cmsMenuInfos = new ArrayList<>(cmsSystemMenuDOS.size());

            cmsSystemMenuDOS.forEach(cmsSystemMenuDO -> {
                CmsMenuInfo cmsMenuInfo = new CmsMenuInfo();
                cmsMenuInfo.setIcon(cmsSystemMenuDO.getMenuPicCode());
                cmsMenuInfo.setName(cmsSystemMenuDO.getMenuName());
                cmsMenuInfo.setPath(cmsSystemMenuDO.getMenuUrl());
                cmsMenuInfo.setComponent(cmsSystemMenuDO.getMenuComponent());

                // 二级菜单转换
                cmsMenuInfo.setChildren(SystemMenuConvertor.convertToSubMenuInfos(cmsSystemMenuDO.getSubSystemMenuDOS()));
                cmsMenuInfos.add(cmsMenuInfo);
            });
            return cmsMenuInfos;
        }
        return Collections.emptyList();
    }

    /**
     * subSystemMenus 转换为 subMenuInfos
     *
     * @author: huangsheng
     * @date: 2017/10/23 下午2:50
     */
    protected static List<SubMenuInfo> convertToSubMenuInfos(final List<SubSystemMenuDO> subSystemMenuDOS) {
        List<SubMenuInfo> subMenuInfos = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(subSystemMenuDOS)) {
            subSystemMenuDOS.forEach(subSystemMenuDO -> {
                subMenuInfos.add(SystemMenuConvertor.convertToSubMenuInfo(subSystemMenuDO));
            });
            return subMenuInfos;
        }
        return Collections.emptyList();
    }

    /**
     * 将 subSystemMenuDO 转换为 SubMenuInfo
     *
     * @author: huangsheng
     * @date: 2017/10/20 下午2:00
     */
    protected static SubMenuInfo convertToSubMenuInfo(final SubSystemMenuDO subSystemMenuDO) {
        if (null == subSystemMenuDO) {
            return null;
        }
        SubMenuInfo subMenuInfo = new SubMenuInfo();
        subMenuInfo.setName(subSystemMenuDO.getMenuName());
        subMenuInfo.setPath(subSystemMenuDO.getMenuUrl());
        subMenuInfo.setComponent(subSystemMenuDO.getMenuComponent());
        return subMenuInfo;
    }

}
